/*
 * @Author: hanxin 
 * @Date: 2021-08-29 17:38:28 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-29 22:35:13
 * dao(data access object)的父类对数据库的基础操作方法,其他的每一个dao具体操作数据库的一张表
 */
package mhl.dao;

import java.util.List;

import mhl.util.jdbcutilsBydruid;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class basicDao<T> {// 泛型指定具体类型(具体javabean)

    private QueryRunner queryRunner = new QueryRunner(); // dbutils作为属性

    /**
     * 通用dml操作方法，针对任意表
     * 
     * @param sql
     * @param parameters
     * @return
     */
    public int dml(String sql, Object... parameters) {
        java.sql.Connection connection = null;
        try {
            connection = jdbcutilsBydruid.getConnection();
            int row = queryRunner.update(connection, sql, parameters);

            return row;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            jdbcutilsBydruid.close(null, null, connection);
        }

    }

    /**
     * 返回多行查询的结果(javabeanList)
     * 
     * @param sql
     * @param clazz
     * @param parameters
     */
    public List<T> queryMulit(String sql, Class<T> clazz, Object... parameters) {
        java.sql.Connection connection = null;
        try {
            connection = jdbcutilsBydruid.getConnection();
            List<T> list = queryRunner.query(connection, sql, new BeanListHandler<>(clazz), parameters);

            return list;

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            jdbcutilsBydruid.close(null, null, connection);
        }

    }

    /**
     * 返回查询到的单行结果
     * 
     * @param sql
     * @param clazz
     * @param parameters
     * @return
     */
    public T query(String sql, Class<T> clazz, Object... parameters) {
        java.sql.Connection connection = null;
        try {
            connection = jdbcutilsBydruid.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(clazz), parameters);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            jdbcutilsBydruid.close(null, null, connection);
        }
    }

   /**
    *返回查询到的单行单列的结果 
    * @param sql
    * @param parameters
    * @return
    */
    public T queryScalar(String sql, Object... parameters) {
        java.sql.Connection connection = null;
        try {
            connection = jdbcutilsBydruid.getConnection();
            return queryRunner.query(connection, sql, new ScalarHandler<>(), parameters);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            jdbcutilsBydruid.close(null, null, connection);
        }
    }
}
